﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuickJob.lib;
using QuickJob.forms;

namespace QuickJob.forms
{
    public partial class SetupDrop : Form
    {

        Config cfg = null;
        DropConfigView dcv;
        DataGridView v;
        int currentcfgIndex { get { return v.SelectedRows[0].Index; } }

        public SetupDrop()
        {
            InitializeComponent();
            v = dataGridView_Config;
            v.CellDoubleClick += v_CellDoubleClick;

            dataGridView_Config.MultiSelect = false;
            dataGridView_Config.AllowUserToAddRows = false;
            dataGridView_Config.AllowUserToDeleteRows = false;
            for (int i = 0; i < this.dataGridView_Config.Columns.Count; i++)
            {
                this.dataGridView_Config.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                this.dataGridView_Config.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            }
        }


        /// <summary>
        /// 双击，加载配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void v_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try {

                if (currentcfgIndex >= cfg.clist.Count)
                {
                    v.Rows[currentcfgIndex].Selected = false;
                    v.Rows[cfg.clist.Count - 1].Selected = true;
                }
                ReloadConfig(currentcfgIndex);

            }
            catch { }
        }


        /// <summary>
        /// 重新加载
        /// </summary>
        /// <param name="cfgIndex"></param>
        void ReloadConfig(int cfgIndex)
        {
            pictureBox1.Controls.Clear();
            if (this.pictureBox1.Image != null) this.pictureBox1.Image.Dispose();
            dcv = new DropConfigView(cfg.clist[cfgIndex], this.pictureBox1);
            ResizeRedraw = true;
            this.dataGridView_Config.Rows[cfgIndex].Selected = true;

        }


        /// <summary>
        /// 表格重新加载
        /// </summary>
        /// <param name="config"></param>
        void DataGridViewReload(Config config)
        {

            cfg = config;
            List<DropConfigItem> lci = cfg.clist;
            v.Rows.Clear();
            foreach (DropConfigItem ci in lci)
            {
                int index = v.Rows.Add();
                v.Rows[index].Cells[0].Value = ci.ConfigName;
                v.Rows[index].Cells[1].Value = ci.PingMing.text;
                v.Rows[index].Cells[2].Value = ci.KuanHao.text;
                v.Rows[index].Cells[3].Value = ci.BuLiao.text;
                v.Rows[index].Cells[4].Value = ci.imgDir;

            }
        }

        public SetupDrop(Config config):this()
        {
            DataGridViewReload(config);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            添加配置ToolStripMenuItem_Click(sender, e);
        }

        private void 添加配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                DropConfigItemCreate c = new DropConfigItemCreate();
                c.onDropConfigItemCreateHandler += c_onDropConfigItemCreateHandler;
                c.Show();

                
            }
            catch { }
        }

        void c_onDropConfigItemCreateHandler(object sender, Ulitily.DropConfigItemCreateEventArgs arg)
        {
            //throw new NotImplementedException();

            if (this.InvokeRequired)
            {
                Ulitily.DropConfigItemCreateHandler handler = new Ulitily.DropConfigItemCreateHandler(c_onDropConfigItemCreateHandler);
                this.BeginInvoke(handler, new object[] { sender, arg });
            }
            else {

                DropConfigItem citem = new DropConfigItem();

                citem.ConfigName = arg.ConfigName;
                citem.PingMing.text = arg.PingMing;
                citem.KuanHao.text = arg.KuanMing;
                citem.BuLiao.text = arg.BuLiao;
                citem.imgDir = arg.Dir;

                citem.PingMing.font = this.Font;
                citem.KuanHao.font = this.Font;
                citem.BuLiao.font = this.Font;

                citem.PingMing.color = Color.Black;
                citem.KuanHao.color = Color.Black;
                citem.BuLiao.color = Color.Black;

                cfg.SaveDropConfigItem(null, citem);

                DataGridViewReload(cfg);

            }

        }



        private void 保存配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentcfgIndex >= cfg.clist.Count)
                {

                    //追加
                }
                else
                {
                    //修改
                    int c = currentcfgIndex;
                    DropConfigItem oitem = cfg.clist[c];
                    DropConfigItem nitem = dcv.ToDropConfigItem();
                    cfg.SaveDropConfigItem(oitem, nitem);
                    DataGridViewReload(cfg);
                         
                    ReloadConfig(c);
                }

            }
            catch { }
        }

        private void 删除配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            try
            {
                if (currentcfgIndex >= cfg.clist.Count)
                {

                }
                else
                {
                    //修改
                    DropConfigItem item = cfg.clist[currentcfgIndex];
                    cfg.DeleteDropConfigItem(item);
                    DataGridViewReload(cfg);
                }

            }
            catch { }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            删除配置ToolStripMenuItem_Click(sender, e);
        }

        private void 品名字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentcfgIndex < cfg.clist.Count && dcv != null)
                {
                    FontDialog fd = new FontDialog();
                    fd.Font = dcv.pingMing.Font;
                    DialogResult r = fd.ShowDialog();

                    if (r == DialogResult.OK || r == DialogResult.Yes)
                    {
                        dcv.pingMing.Font = fd.Font;
                        dcv.pingMing.ForeColor = Color.Black;
                    }
                }

            }
            catch { }
        }

        private void 款号字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentcfgIndex < cfg.clist.Count && dcv != null)
                {
                    FontDialog fd = new FontDialog();
                    fd.Font = dcv.kuanHao.Font;
                    DialogResult r = fd.ShowDialog();

                    if (r == DialogResult.OK || r == DialogResult.Yes)
                    {
                        dcv.kuanHao.Font = fd.Font;
                        dcv.kuanHao.ForeColor = Color.Black;
                    }
                }

            }
            catch { }
        }

        private void 布料字体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (currentcfgIndex < cfg.clist.Count && dcv != null)
                {
                    FontDialog fd = new FontDialog();
                    fd.Font = dcv.buLiao.Font;
                    DialogResult r = fd.ShowDialog();

                    if (r == DialogResult.OK || r == DialogResult.Yes)
                    {
                        dcv.buLiao.Font = fd.Font; 
                        dcv.buLiao.ForeColor = Color.Black;
                    }
                }

            }
            catch { }
        }

        private void 模板路径ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.Title = "选择　洗水唛　的图片文件：xxx.jpeg  or jpg";
            if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (file.CheckFileExists == false)
                {
                    MessageBox.Show("文件无效");
                }
                else
                {
                    dcv.pictureDir = file.FileName;
                    if (pictureBox1.Image != null){pictureBox1.Image.Dispose();   }
                    pictureBox1.Image = Image.FromFile(dcv.pictureDir);
                }
            }
        }

        private void 图片另存为ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(pictureBox1.Size.Width,pictureBox1.Size.Height);
            pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Size.Width, pictureBox1.Size.Height));
            SaveFileDialog file = new SaveFileDialog();
            file.FileName = "合格证.jpg";
            DialogResult result = file.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK || result == System.Windows.Forms.DialogResult.Yes) {

                string path = file.FileName;
                b.Save(path);
            
            }
           // b.Save(@"C:\Users\Administrator\Desktop\kaihua.jpeg");
        }
    }
}
